package com.party.controller;


import com.party.entity.Pmember;
import com.party.service.IPmemberService;
import com.party.util.Result;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-06-10
 */
@Controller
@RequestMapping("/pmember")
public class PmemberController {

    @Autowired
    private IPmemberService pmemberService;

    @PostMapping
    @ResponseBody
    public Result addMember(Pmember pmember) {
        Result<Object> result = new Result<>();
        result.setCode(200);
        if (pmember.getId() != null) {
//            更新
            pmemberService.updateById(pmember);
            return result;
        }
        pmemberService.save(pmember);
        return result;
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result deleteMember(@PathVariable int id) {
        pmemberService.removeById(id);
        Result<Object> result = new Result<>();
        result.setCode(200);
        return result;
    }

    @GetMapping("/{id}")
    public String detail(@PathVariable String id, Model model) {
        Pmember pmember = pmemberService.getById(Integer.parseInt(id));
        model.addAttribute("member", pmember);
        return "detail";
    }

}
